package com.example.app1;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;

import com.example.handler.ClientHandler;
import com.example.listeners.MyOnClickListener;
import com.example.listeners.MyLongClickListener;
import com.example.listeners.MyOnTouchListener;

public class MainActivity extends Activity {

	public Object lock;
	private ClientHandler client;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setupRemoteUI();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	} 

	private void setupRemoteUI() {
		lock = new Object();

		client = ClientHandler.getInstance(this);
		client.init();

		synchronized (lock) {
			try {
				lock.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		displayBitmap(client.getImage());
	}

	private void displayBitmap(Bitmap image) {

		ImageView imageView = new ImageView(this);

		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		imageView.setImageBitmap(image);

		RelativeLayout rl = (RelativeLayout) findViewById(R.id.pepa);

		rl.addView(imageView, lp);
		
		// add action listeners
		imageView.setOnClickListener(new MyOnClickListener(client));
		imageView.setOnLongClickListener(new MyLongClickListener(client));
		imageView.setOnTouchListener(new MyOnTouchListener(client));
	}
	
	public ClientHandler getClientHandler() {
		return client;
	}
}
